package com.business_clean.viewmodel.request

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.business_clean.app.App
import com.business_clean.app.config.Constant
import com.business_clean.app.network.NetUrl
import com.business_clean.data.mode.address.AddressDetailEntity
import com.business_clean.data.mode.group.GroupMangerEntity
import com.business_clean.data.mode.group.GroupMangerListEntity
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.ext.rxHttpRequest
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class GroupViewModel : BaseViewModel() {

    var groupMangerEntity = MutableLiveData<GroupMangerEntity>()
    var delGroup = MutableLiveData<Any>()
    var saveGroup = MutableLiveData<Any>()
    var getOneGroup = MutableLiveData<AddressDetailEntity>()

    /**
     * 获取全部
     */
    fun requestGroupAll(page: Int, project_uuid: String? = "") {
        rxHttpRequest {
            onRequest = {
                groupMangerEntity.value = RxHttp.get(NetUrl.GET_GROUP_ALL)
                    .add("page", page)
                    .add("size", Constant.PAGE_SIZE)
                    .add("project_uuid", if (!TextUtils.isEmpty(project_uuid)) project_uuid else App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<GroupMangerEntity>().await()
            }
        }
    }

    /**
     * 获取小组详情
     */
    fun requestGroupDetail(uuid: String) {
        rxHttpRequest {
            onRequest = {
                getOneGroup.value = RxHttp.get(NetUrl.GET_GROUP_DETAIL)
                    .add("uuid", uuid)
                    .add("project_uuid", App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<AddressDetailEntity>().await()
            }
        }
    }

    /**
     * 新建小组
     */
    fun requestSaveGroup(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                saveGroup.value = RxHttp.get(NetUrl.SAVE_GROUP)
                    .addAll(hashMap)
                    .toResponse<Any>().await()
            }
        }
    }

    /**
     * 删除小组
     */
    fun requestDelGroup(uuid: String) {
        rxHttpRequest {
            onRequest = {
                delGroup.value = RxHttp.get(NetUrl.DEL_GROUP)
                    .add("uuid", uuid)
                    .add("project_uuid", App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<Any>().await()
            }
        }
    }
}